<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"
				content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
	<title>部门管理</title>
	<!-- lay ui css -->
	<link th:href="@{/component/layui/css/layui.css}" rel="stylesheet">
	<!-- 自定义 css -->
	<link rel="stylesheet" th:href="@{/dimples/css/dimples.css}"/>
	<link rel="stylesheet" th:href="@{/dimples/css/eleTree.css}"/>
	<style>
		#dimples-dept .dimples-container .layui-card {
			height: 600px;
		}

		#dimples-dept .dimples-container .layui-card .layui-card-body {
			height: 442px !important;
		}
	</style>
</head>
<body>
<div class="layui-fluid layui-anim dimples-anim" id="dimples-dept" lay-title="部门管理">
	<div class="layui-row layui-col-space8 dimples-container">
		<div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
			<div class="layui-card">
				<div class="layui-card-body dimples-table-full">
					<form class="layui-form layui-table-form" lay-filter="dept-table-form"
								id="dept-table-form">
						<div class="layui-row">
							<div class="layui-col-md8 layui-col-sm9 layui-col-xs9">
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label layui-form-label-sm">名称</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="deptName" autocomplete="off" class="layui-input">
											</label>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-col-md4 layui-col-sm12 layui-col-xs12 table-action-area">
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
									<i class="layui-icon">&#xe848;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
									<i class="layui-icon">&#xe79b;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
										 shiro:hasAnyPermissions="dept:add,dept:delete,dept:export">
									<i class="layui-icon">&#xe875;</i>
								</div>
							</div>
						</div>
					</form>
					<!-- 树形菜单 -->
					<div class="dept-tree" lay-filter="deptTree"></div>
				</div>
			</div>
		</div>
		<div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
			<div class="layui-card">
				<div class="layui-card-header" id="form-header">新增部门</div>
				<div class="layui-card-body dimples-table-full">
					<form class="layui-form layui-table-form" action="" lay-filter="dept-form">
						<div class="layui-form-item">
							<label class="layui-form-label dimples-form-item-require">
								ID：
							</label>
							<div class="layui-input-block">
								<label>
									<input type="text" name="deptId" class="layui-input" disabled="disabled">
								</label>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">上级ID：</label>
							<div class="layui-input-block">
								<label>
									<input type="text" value="" name="parentId" autocomplete="off"
												 class="layui-input">
								</label>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label dimples-form-item-require">部门名称：</label>
							<div class="layui-input-block">
								<label>
									<input type="text" name="deptName" autocomplete="off" class="layui-input"
												 minlength="2" maxlength="10" lay-verify="range">
								</label>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">部门排序：</label>
							<div class="layui-input-block">
								<label>
									<input type="text" name="orderNum" autocomplete="off" class="layui-input"
												 lay-verify="number">
								</label>
							</div>
						</div>
						<button type="reset" class="layui-btn dimples-hide" id="reset-form"></button>
						<button class="layui-btn dimples-hide" lay-submit="" lay-filter="dept-form-submit"
										id="submit-form"></button>
					</form>
				</div>
				<div class="layui-card-footer">
					<button class="layui-btn layui-btn-normal" id="submit">保存</button>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
<!-- lay ui js -->
<script type="text/javascript" th:src="@{/component/layui/layui.js}"></script>
<script th:inline="javascript">
    var currentUser = {};
    currentUser.roleSet = [[${roles}]];
    currentUser.permissionSet = [[${permissions}]];
    console.log("用户角色：" + currentUser.roleSet);
    console.log("用户权限：" + currentUser.permissionSet);
</script>
<script data-th-inline="javascript" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'validate', 'pearDimples', 'form', 'eleTree'], function () {
        var $ = layui.jquery,
            dimples = layui.pearDimples,
            form = layui.form,
            validate = layui.validate,
            eleTree = layui.eleTree,
            dropdown = layui.dropdown,
            $view = $('#dimples-dept');
        var
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $header = $view.find('#form-header'),
            $searchForm = $view.find('#dept-table-form'),
            $deptName = $searchForm.find('input[name="deptName"]'),
            _currentDeptData,
            _deptTree;

        form.verify(validate);
        form.render();

        renderDeptTree();

        eleTree.on("nodeClick(deptTree)", function (d) {
            $header.text('修改部门');
            var data = d.data.currentData.data;
            _currentDeptData = data;
            form.val("dept-form", {
                "deptName": data.deptName,
                "orderNum": data.orderNum,
                "createTime": data.createTime,
                "parentId": data.parentId,
                "deptId": data.deptId
            });
        });

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                if (name === 'add') {
                    reset();
                    var selected = _deptTree.getChecked(false, true);
                    if (selected.length > 1) {
                        dimples.alert.warn('只能选择一个节点作为父级！');
                        return;
                    }
                    form.val("dept-form", {
                        "parentId": selected[0] ? selected[0].id : ''
                    });
                }
                if (name === 'delete') {
                    var checked = _deptTree.getChecked(false, true);
                    if (checked.length < 1) {
                        dimples.alert.warn('请勾选需要删除的部门');
                        return;
                    }
                    var deptIds = [];
                    layui.each(checked, function (key, item) {
                        deptIds.push(item.id)
                    });
                    dimples.modal.confirm('提示', '当您点击确定按钮后，这些记录将会被彻底删除，如果其包含子记录，也将一并删除！', function () {
                        dimples.post('/dept/delete/' + deptIds.join('|'), null, function () {
                            dimples.alert.success('删除成功！');
                            reloadDeptTree();
                            reset();
                        })
                    });
                }
                if (name === 'export') {
                    dimples.download('/dept/excel', getQueryParams(), '部门信息表.xlsx');
                }
            },
            options: [{
                name: 'add',
                title: '新增部门',
                perms: 'dept:add'
            }, {
                name: 'delete',
                title: '删除部门',
                perms: 'dept:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'dept:export'
            }]
        });

        $view.on('click', '#submit', function () {
            $view.find('#submit-form').trigger('click');
        });

        $reset.on('click', function () {
            $deptName.val('');
            reloadDeptTree();
            reset();
        });

        $query.on('click', function () {
            reloadDeptTree();
        });

        function getQueryParams() {
            return {
                "deptName": $deptName.val().trim()
            }
        }

        function reset() {
            $header.text('新增部门');
            $view.find('#reset-form').trigger('click');
        }

        function renderDeptTree() {
            _deptTree = eleTree.render({
                elem: '.dept-tree',
                url: '/dept/tree',
                showCheckbox: true,
                where: {
                    "deptName": $deptName.val().trim(),
                    "invalidate_ie_cache": new Date()
                },
                request: {
                    name: 'name',
                    key: "id",
                    checked: "checked",
                    data: 'data'
                },
                response: {
                    statusName: "code",
                    statusCode: 200,
                    dataName: "data"
                }
            });
            return _deptTree;
        }

        function reloadDeptTree() {
            _deptTree = renderDeptTree();
        }

        form.on('submit(dept-form-submit)', function (data) {
            if (data.field.deptId && $header.text().indexOf('修改') !== -1) {
                if (dimples.nativeEqual(data.field, _currentDeptData)) {
                    dimples.alert.warn('数据未作任何修改！');
                    return false;
                }
                dimples.post('/dept/update', data.field, function () {
                    dimples.alert.success('修改成功');
                    reloadDeptTree();
                    reset();
                })
            } else {
                dimples.post('/dept', data.field, function () {
                    dimples.alert.success('新增成功');
                    reloadDeptTree();
                    reset();
                })
            }
            return false;
        });
    });
</script>
</html>











